package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.DamageListDao;
import com.atguigu.jxc.dao.DamageListGoodsDao;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import com.atguigu.jxc.util.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DamageListGoodsServiceImpl implements DamageListGoodsService {
    @Autowired
    private DamageListGoodsDao damageListGoodsDao;
    @Autowired
    private DamageListDao damageListDao;
    @Autowired
    private GoodsDao goodsDao;

    /**
     * 保存报损单以及报损商品
     * @param damageList
     * @param damageListGoodsStr
     * @return
     */
    @Override
    @Transactional
    public ServiceVO save(DamageList damageList, String damageListGoodsStr) {
        if(StringUtil.isNotEmpty(damageListGoodsStr)){
            damageList.setUserId(1);
            //添加报损单
            damageListDao.save(damageList);
            //获取报损单单号
            Integer DamageListId =damageListDao.getByNum(damageList.getDamageNumber());
            Gson gson = new Gson();
            List<DamageListGoods> list= gson.fromJson(damageListGoodsStr, new TypeToken<List<DamageListGoods>>(){}.getType());
            //循环遍历报损商品，一件件添加进t_damage_list_goods
            for (DamageListGoods damageListGoods : list) {

                Goods good = goodsDao.getById(damageListGoods.getGoodsId());
                int total = good.getInventoryQuantity().intValue();
                if(total < damageListGoods.getGoodsNum()){
                    return new ServiceVO(400,"库存小于报损数量");
                }
                //减库存
                goodsDao.updateQuantity(damageListGoods.getGoodsId(),total-damageListGoods.getGoodsNum());
                damageListGoods.setDamageListId(DamageListId);
                log.info(damageListGoods.toString());
                damageListGoodsDao.save(damageListGoods);
            }

            return new ServiceVO(100,"请求成功");
        }
        return new ServiceVO(400,"报损商品不能为空");

    }

    /**
     * 查询报损单
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public List<DamageList> list(String sTime, String eTime) {
        Date start = null;
        Date end = null;
        try {
            String format = "yyyy-MM-dd";
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime, format);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return damageListDao.list(start,end);
    }

    @Override
    public List<DamageListGoods> goodsList(Integer damageListId) {
        return damageListGoodsDao.list(damageListId);
    }
}
